<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/2/18
 * Time: 下午9:22
 */
namespace Common\Model;

class ClassificationModel extends BaseModel
{
    /**
     * 获取所有子分类ID和当前分类ID
     * @param $id 当前分类ID
     * @return string 返回一个所有ID组成的字符串
     */
    public function get_ids($id)
    {
        $child_ids = $this->get_field('id',['pid'=>$id],true);
        if(empty($child_ids)){
            return '';
        }
        $ids = '';
        foreach($child_ids as $v){
            $ids .= ','.$v;
            $ids .= $this->get_ids($v);
        }
        return $ids;
    }

    /**
     * 获取所有父级信息和自己信息
     * @param $pid
     */
    public function get_all_p($id)
    {
        //获取自己信息
        $self_info = $this->get_field('id,classification,pid',['id'=>$id]);
        if($self_info[$id]['pid'] == 0){
            return $self_info;
        }
        $all_p = $self_info;
        //获取父级信息
        $p_info = $this->get_field('id,classification,pid',['id'=>$self_info[$id]['pid']]);
        $all_p = array_merge($all_p,$p_info);
        foreach($p_info as $v){
            if($v['pid'] == 0){
                return $all_p;
            }
            //如果还有爷爷就获取爷爷信息
            $y_info = $this->get_field('id,classification,pid',['id'=>$v['pid']]);
            $all_p = array_merge($all_p,$y_info);
        }
        return $all_p;
    }

    /**
     * 根据ID获取所有的子分类信息
     * @param $id
     */
    public function get_all_c($id)
    {
        //获取孩子信息
        $child_info = $this->get_many('pid='.$id,'id,classification');
        $all_child = $child_info;

        foreach($child_info as $k=>$v){
            //循环获得孙子信息
            $s_info = $this->get_many('pid='.$v['id'],'id,classification');
            $all_child[$k]['s_info'] = $s_info;
        }
        return $all_child;
    }
}